/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#include "Packet.hpp"
#include "Xern/DataStream.hpp"

namespace XernProtocol
{
	Packet::Packet()
	{
		// Initialize an empty packet.
		flags = 0;
		sequenceNumber = 0;
		sourceSession = 0;
		destinationSession = 0;
		channel = 0;
		sentTime = 0;
		packetSize = 0;
		payloadSize = 0;
		payloadData = NULL;
		ackCount = 0;
		ackData = NULL;
		resendCount = 0;
	}

	Packet::~Packet()
	{
	}

	void Packet::DecodePacket()
	{
		// Read the headers data.
		DataStream in(packetSize, packetData.c_array());
		in >> flags >> sequenceNumber >> sourceSession;
		in >> destinationSession >> channel >> sentTime;

		// Read the packet payload.
		in >> payloadSize;
		if(payloadSize > 0)
		{
			payloadData = &packetData[in.GetPosition()];
			in.Skip(payloadSize);
		}
		else
		{
			payloadData = NULL;
		}

		// Read the appended acks.
		if(flags & FL_ACKS)
		{
			in >> ackCount;
			if(ackCount > 0)
				ackData = &packetData[in.GetPosition()];
			else
				ackData = NULL;
		}
		else
		{
			ackData = NULL;
		}
	}

	void Packet::WriteHeader()
	{
		// Write the headers data.
		DataStream out(MAX_PACKET_SIZE, packetData.c_array());
		out << flags << sequenceNumber << sourceSession;
		out << destinationSession << channel << sentTime;

		// Update the packet size.
		packetSize = out.GetPosition();
		payloadData = &packetData[packetSize];
	}

	void Packet::UpdateHeader()
	{
		// Write the headers data.
		DataStream out(MAX_PACKET_SIZE, packetData.c_array());
		out << flags << sequenceNumber << sourceSession;
		out << destinationSession << channel << sentTime;
	}

	void Packet::WritePayload(size_t size, const void *data)
	{
		assert(payloadData); // The header must have been written.
		assert(size + 2 <= MAX_PACKET_SIZE - packetSize); // Avoid overflow.

		// Update the sizes.
		payloadSize = size;
		packetSize += size + 2;

		// Send the payload size and the payload.
		DataStream out(MAX_PACKET_SIZE, payloadData);
		out << payloadSize;
		if(data)
			out.Write(size, data);

		// Update the payload pointer
		payloadData += 2; // Skip the payload size.

		// Prepare the acks.
		if(flags & FL_ACKS)
			ackData = &packetData[packetSize];
	}

	void Packet::WriteAcks(size_t numacks, const u32 *acks)
	{
		assert(ackData);
		assert(numacks*4 + 1 <= MAX_PACKET_SIZE - packetSize);

		// Update the sizes.
		ackCount = numacks;
		packetSize += numacks*4 + 1;

		// Send the acknowledges.
		DataStream out(MAX_PACKET_SIZE, ackData);
		out << ackCount;
		for(size_t i = 0; i < numacks; i++)
			out << acks[i];

		// Update the pointers.
		ackData += 1; // Skip the count.
	}

}; // namespace XernProtocol
